package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.model.MemberRebate;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.MemberRebateService;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "memberrebate")
public class MemberRebateController {

    @Autowired
    private MemberRebateService memberRebateService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @RequestMapping(value = "rebate", method = RequestMethod.GET)
    public ModelAndView list() {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("memberrebate")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("memberrebate/rebate");

            MemberRebate memberRebate = memberRebateService.findByCompany(principal.getCompanyId());
            if (null != memberRebate) {
                mv.addObject("rebate", memberRebate);
            } else {
                mv.addObject("rebate", new MemberRebate());
            }

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody
    Result<MemberRebate> update(MemberRebate memberRebate, Model model) {

        Result<MemberRebate> vo = new Result<MemberRebate>();

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("memberrebate")) {

            if (null == memberRebate.getId()) {
                memberRebate.setCompanyId(principal.getCompanyId());
                memberRebateService.save(memberRebate);
            } else {
                MemberRebate local = memberRebateService.findOne(memberRebate.getId());

                local.setType(memberRebate.getType());
                local.setLevel(memberRebate.getLevel());
                if (memberRebate.getType().equals(0)) {
                    local.setDisposableMoney1(memberRebate.getDisposableMoney1());
                    local.setDisposableMoney2(memberRebate.getDisposableMoney2());
                    local.setDisposableMoney3(memberRebate.getDisposableMoney3());
                } else if (memberRebate.getType().equals(1)) {
                    local.setCommission1(memberRebate.getCommission1());
                    local.setCommission2(memberRebate.getCommission2());
                    local.setCommission3(memberRebate.getCommission3());

                    local.setMoney1(memberRebate.getMoney1());
                    local.setMoney2(memberRebate.getMoney2());
                    local.setMoney3(memberRebate.getMoney3());

                    local.setMemberRebateType1(memberRebate.getMemberRebateType1());
                    local.setMemberRebateType2(memberRebate.getMemberRebateType2());
                    local.setMemberRebateType3(memberRebate.getMemberRebateType3());
                }

                memberRebateService.update(local);
            }

            String detail = "【%s】修改了客户推荐提成";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            vo.setSuccess(true);
        } else {
            vo.setSuccess(false);
            vo.setMessage("您没有该权限");
        }

        return vo;

    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {

        SimpleDateFormat dateFormat = com.ybkj.daijia.constant.DateConstant.DATE_TIME;
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }

}

